<?php

/*
 * @author Dudnex
 * @status NOT TESTED, NOT APPROVED
 */

/* Application class */
require_once 'Application.php';
require_once 'DB/db_data_loader.php';

$all_load_m = get_all_is_load_on_measures();
$all_load_a = get_all_is_apps_on_measures();
$all_load = get_all_is_load();

/*
 * Changes global <$all_is_load> variable if there was any changes
 */

function is_load_changed() {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $all_load_m = get_all_is_load_on_measures();
    $all_load = get_all_is_load();
}

/*
 * Returns list of subdivisions used in <$measure_code> administration
 * 
 * @params <$measure_code>
 * @return array["SUB1", "SUB2", ...];
 */

function get_used_subdivisions($measure_code) {
    $result = mysql_query("SELECT subdivision_code FROM support_administration 
        WHERE measure_code = '" . $measure_code . "'");
    $used_subdivisions = array();

    print_r(mysql_error());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $used_subdivisions[$index] = $row["subdivision_code"];
        $index++;
    }

    mysql_free_result($result);
    return $used_subdivisions;
}

/*
 * Returns list of information systems used in <$subdivision>
 * 
 * @params <$subdivision>
 * @return array["IS1", "IS2", ...]
 */

function get_used_is($subdivision) {
    $result = mysql_query("SELECT is_code FROM is_subdivisions 
        WHERE subdivision_code = '" . $subdivision . "'");
    $used_is = array();

    print_r(mysql_error());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $used_is[$index] = $row["is_code"];
        $index++;
    }

    mysql_free_result($result);
    return $used_is;
}

/*
 * Returns list of informations systems load 
 * 
 * @return array[IS][APPLICATION_INDEX]
 */

function get_all_is_load() {
    $is_load = array();
    $all_historical_quantities = get_historical_quantities();

    $history_entrys = count($all_historical_quantities["DATE"]);
    $subs = 0;
    for ($i = 0; $i < $history_entrys; $i++) {
        $date = $all_historical_quantities["DATE"][$i];
        $measure_code = $all_historical_quantities["MEASURE_CODE"][$i];
        $received = $all_historical_quantities["RECEIVED"][$i];
        $administrated = $all_historical_quantities["ADMINISTRATED"][$i];

        $application = new Application($date, $measure_code, $received, $administrated);
        $is_load[$i] = $application;
    }

    return $is_load;
}

/*
 * @return array[MEASURE][IS] => time
 */

function get_all_is_load_on_measures() {
    $all_load = array();

    $measures = get_support_measures();
    foreach ($measures["CODE"] as $row => $value) {
        $all_load[$value] = array();
        $ises = get_information_systems();
        foreach ($ises["CODE"] as $row2 => $value2) {
            $all_load[$value][$value2] = get_is_load($value, $value2);
        }
    }

    return $all_load;
}

/*
 * @return array[MEASURE][IS] => time
 */

function get_all_is_apps_on_measures() {
    $all_apps = array();

    $measures = get_support_measures();
    foreach ($measures["CODE"] as $row => $value) {
        $all_apps[$value] = array();
        $ises = get_information_systems();
        foreach ($ises["CODE"] as $row2 => $value2) {
            $all_apps[$value][$value2] = 0;
            $all_apps[$value][$value2] += get_is_load_on_app($value, $value2);
        }
    }

    return $all_apps;
}

/*
 * Get is application load on measure
 * 
 * @return hours
 */

function get_is_load_on_app($measure_code, $is) {
    $is_load = 0;

    $used_subs = get_used_subdivisions($measure_code);
    $sub_entrys = count($used_subs);
    for ($i = 0; $i < $sub_entrys; $i++) {
        if (is_information_system_in_subdivision($is, $used_subs[$i])) {
            $is_load += 1;
        }
    }

    return $is_load;
}

/*
 * Get is time load on measure
 * 
 * @return hours
 */

function get_is_load($measure_code, $is) {
    $is_load = 0;

    $used_subs = get_used_subdivisions($measure_code);
    $sub_entrys = count($used_subs);
    for ($i = 0; $i < $sub_entrys; $i++) {
        if (is_information_system_in_subdivision($is, $used_subs[$i])) {
            $is_load += get_subdivision_work_on_measure($used_subs[$i], $measure_code);
        }
    }

    return $is_load;
}

/* 
 * $return boolean if <IS> is used by <subdivision>
 */

function is_information_system_in_subdivision($is, $subdivision) {
    $ises = get_used_is($subdivision);
    foreach ($ises as $is_code) {
        if ($is_code == $is) {
            return true;
        }
    }
    return false;
}

/*
 * Returns time in hours for <$subdivision> to administrate <$support_measure>
 * 
 * @params <$subdivision, $support_measure>
 * @return hours
 */

function get_subdivision_work_on_measure($subdivision, $support_measure) {
    $result = mysql_query("SELECT time FROM support_administration 
        WHERE subdivision_code = '" . $subdivision . "' and 
            measure_code = '" . $support_measure . "'");
    print_r(mysql_error());
    $row = mysql_fetch_array($result);
    $hours = $row["time"];
    mysql_free_result($result);
    return $hours;
}

/*
 * Returns time in hours of <is> administration worktime in total
 * 
 * @params <$is>
 * @return hours
 */

function get_is_work($is) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->administrated;
    }

    return $working_hours;
}

/*
 * Returns time in hours of <is> administration worktime for <$subdivision>
 * 
 * @params <$subdivision, $is>
 * @return hours
 */

function get_is_work_by_sub($subdivision, $is) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        if ($all_load[$i]->subdivision == $subdivision) {
            $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->administrated;
        }
    }

    return $working_hours;
}

/*
 * Returns time in hours of <is> worktime in total
 * 
 * @params <$is>
 * @return hours
 */

function get_total_is_work($is) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->received;
    }

    return $working_hours;
}

/*
 * Returns time in hours of <is> worktime for <$subdivision>
 * 
 * @params <$subdivision, $is>
 * @return hours
 */

function get_total_is_work_by_sub($subdivision, $is) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        if ($all_load[$i]->subdivision == $subdivision) {
            $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->received;
        }
    }

    return $working_hours;
}

/*
 * Returns time in hours of <$is> administration work time for <$subdivision> at <$date> given year -> month
 * $date format YYYY-MM-DD (can be any other delimiter, day can be skipped)
 * 
 * @params <$subdivision, $is, $date>
 * @return hours
 */

function get_is_work_by_sub_by_month($subdivision, $is, $date) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        if ($all_load[$i]->subdivision == $subdivision) {
            $current_application_date = $all_load[$i]->date;

            if (substr($date, 0, 4) == substr($current_application_date, 0, 4) &&
                    substr($date, 5, 2) == substr($current_application_date, 5, 2)) {

                $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->administrated;
            }
        }
    }

    return $working_hours;
}

/*
/*
 * Returns time in hours of <$is> work time for <$subdivision> at <$date> given year -> month in total
 * $date format YYYY-MM-DD (can be any other delimiter, day can be skipped)
 * 
 * @params <$subdivision, $is, $date>
 * @return hours
 */

function get_total_is_work_by_sub_by_month($subdivision, $is, $date) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        if ($all_load[$i]->subdivision == $subdivision) {
            $current_application_date = $all_load[$i]->date;

            if (substr($date, 0, 4) == substr($current_application_date, 0, 4) &&
                    substr($date, 5, 2) == substr($current_application_date, 5, 2)) {

                $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->received;
            }
        }
    }

    return $working_hours;
}

/*
 * Returns time in hours of <$is> administration work time at <$date> given year -> month
 * $date format YYYY-MM-DD (can be any other delimiter, day can be skipped)
 * 
 * @params <$is, $date>
 * @return hours
 */

function get_is_work_by_month($is, $date) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;

    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        $current_application_date = $all_load[$i]->date;

        if (substr($date, 0, 4) == substr($current_application_date, 0, 4) &&
                substr($date, 5, 2) == substr($current_application_date, 5, 2)) {

            $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->administrated;
        }
    }
    
    return $working_hours;
}

/*
 * Returns time in hours of <$is> work time at <$date> given year -> month in total
 * $date format YYYY-MM-DD (can be any other delimiter, day can be skipped)
 * 
 * @params <$is, $date>
 * @return hours
 */

function get_total_is_work_by_month($is, $date) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $working_hours = 0;

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        $current_application_date = $all_load[$i]->date;

        if (substr($date, 0, 4) == substr($current_application_date, 0, 4) &&
                substr($date, 5, 2) == substr($current_application_date, 5, 2)) {

            $working_hours += $all_load_m[$all_load[$i]->measure][$is] * $all_load[$i]->received;
        }
    }

    return $working_hours;
}

/*
 * Returns time in hours of <$is> left work time in total
 * 
 * @params <$is>
 * @return hours
 */

function get_total_left_is_work($is) {
    $left_over_hours = get_total_is_work($is) - get_is_work($is);
    if ($left_over_hours >= 0) {
        return $left_over_hours;
    } else {
        return 0;
    }
}

/*
 * Returns time in hours of <$is> left work time in <$subdivision>
 * 
 * @params <$is>
 * @return hours
 */

function get_total_left_is_work_by_sub($subdivision, $is) {
    $left_over_hours = get_total_is_work($subdivision, $is) - get_is_work($subdivision, $is);
    if ($left_over_hours >= 0) {
        return $left_over_hours;
    } else {
        return 0;
    }
}

/*
 * Returns time in hours of <$is> planned work time at <$month>
 * $month format 01, 02, 03, ...
 * 
 * @params <$is, $month>
 * @return hours
 */

function get_planned_is_work_by_month($is, $month) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $planned_working_hours = 0;
    $diff_month_count = 0;
    $current_year = "";

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        $current_application_date = $all_load[$i]->date;
        if (substr($current_application_date, 5, 2) == $month) {

            $planned_working_hours += $all_load[$i]->received * $all_load_m[$all_load[$i]->measure][$is];
            if ($current_year != substr($current_application_date, 0, 4)) {
                $current_year = substr($current_application_date, 0, 4);
                $diff_month_count++;
            }
        }
    }

    if ($diff_month_count == 0) {
        return 0;
    } else {
        return $planned_working_hours / $diff_month_count;
    }
}

/*
 * Returns time in hours of <$is> planned work time at <$month> for <$subdivision>
 * $month format 01, 02, 03, ...
 * 
 * @params <$subdivision, $is, $month>
 * @return hours
 */

function get_planned_is_work_by_sub_by_month($subdivision, $is, $month) {
    GLOBAL $all_load_m;
    GLOBAL $all_load;
    $planned_working_hours = 0;
    $diff_month_count = 0;
    $current_year = "";

    $is_application_entrys = count($all_load);
    for ($i = 0; $i < $is_application_entrys; $i++) {
        if ($all_load[$i]->subdivision == $subdivision) {
            $current_application_date = $all_is_load[$is][$i]->date;

            if (substr($current_application_date, 5, 2) == $month) {

                $planned_working_hours += $all_load[$i]->received * $all_load_m[$all_load[$i]->measure][$is];
                if ($current_year != substr($current_application_date, 0, 4)) {
                    $current_year = substr($current_application_date, 0, 4);
                    $diff_month_count++;
                }
            }
        }
    }

    if ($diff_month_count == 0) {
        return 0;
    } else {
        return $planned_working_hours / $diff_month_count;
    }
}

/*
 * Returns administrated application count of <$is> in total
 * 
 * @params <$is>
 * @return application count
 */

function get_is_application_count($is) {
    GLOBAL $all_load_a;
    GLOBAL $all_load;
    $application_count = 0;

    $is_application_entrys = count($all_load);

    for ($i = 0; $i < $is_application_entrys; $i++) {
        $application_count += $all_load[$i]->administrated * $all_load_a[$all_load[$i]->measure][$is];
    }

    return $application_count;
}

/*
 * Returns received application count of <$is> in total
 * 
 * @params <$is>
 * @return application count
 */

function get_total_is_application_count($is) {
    GLOBAL $all_load_a;
    GLOBAL $all_load;
    $application_count = 0;

    $is_application_entrys = count($all_load);

    for ($i = 0; $i < $is_application_entrys; $i++) {
        $application_count += $all_load[$i]->received * $all_load_a[$all_load[$i]->measure][$is];
    }

    return $application_count;
}

/*
 * Returns administrated application count of <$is> at <$date> given year -> month
 * $date format YYYY-MM-DD (can be any other delimiter, day can be skipped)
 * 
 * @params <$is, $date>
 * @return application count
 */

function get_is_application_count_by_month($is, $date) {
    GLOBAL $all_load_a;
    GLOBAL $all_load;
    $application_count = 0;

    $is_application_entrys = count($all_load);

    for ($i = 0; $i < $is_application_entrys; $i++) {
        $current_application_date = $all_load[$i]->date;

        if (substr($date, 0, 4) == substr($current_application_date, 0, 4) &&
                substr($date, 5, 2) == substr($current_application_date, 5, 2)) {
            $application_count += $all_load[$i]->administrated * $all_load_a[$all_load[$i]->measure][$is];
        }
    }

    return $application_count;
}

/*
 * Returns received application count of <$is> at <$date> given year -> month
 * $date format YYYY-MM-DD (can be any other delimiter, day can be skipped)
 * 
 * @params <$is, $date>
 * @return application count
 */

function get_total_is_application_count_by_month($is, $date) {
    GLOBAL $all_load_a;
    GLOBAL $all_load;
    $application_count = 0;

    $is_application_entrys = count($all_load);

    for ($i = 0; $i < $is_application_entrys; $i++) {
        $current_application_date = $all_load[$i]->date;

        if (substr($date, 0, 4) == substr($current_application_date, 0, 4) &&
                substr($date, 5, 2) == substr($current_application_date, 5, 2)) {
            $application_count += $all_load[$i]->received * $all_load_a[$all_load[$i]->measure][$is];
        }
    }

    return $application_count;
}

/*
 * Returns planned application count of <$is> at <$month>
 * $month format 01, 02, 03, ...
 * 
 * @params <$is, $month>
 * @return application count
 */

function get_planned_is_application_count_by_month($is, $month) {
    GLOBAL $all_load;
    GLOBAL $all_load_a;
    $planned_application_count = 0;

    $diff_month_count = 0;
    $current_year = "";
    $is_application_entrys = count($all_load);

    for ($i = 0; $i < $is_application_entrys; $i++) {
        $current_application_date = $all_load[$i]->date;
        if (substr($current_application_date, 5, 2) == $month) {
            $planned_application_count += $all_load[$i]->received * $all_load_a[$all_load[$i]->measure][$is];
        }
        if ($current_year != substr($current_application_date, 0, 4)) {
            $current_year = substr($current_application_date, 0, 4);
            $diff_month_count++;
        }
    }

    if ($diff_month_count == 0) {
        return 0;
    } else {
        return $planned_application_count / $diff_month_count;
    }
}

/*
 * Returns planned application count of <$is> at <$month> for <$subdivision>
 * $month format 01, 02, 03, ...
 * 
 * @params <$is, $month>
 * @return application count
 */

function get_planned_is_application_count_by_sub_by_month($subdivision, $is, $month) {
    GLOBAL $all_load;
    GLOBAL $all_load_a;
    $planned_application_count = 0;

    $diff_month_count = 0;
    $current_year = "";
    $is_application_entrys = count($all_load);

    for ($i = 0; $i < $is_application_entrys; $i++) {
        if ($all_load[$i]->subdivision == $subdivision) {
            $current_application_date = $all_load[$i]->date;
            if (substr($current_application_date, 5, 2) == $month) {
                $planned_application_count += $all_load[$i]->received * $all_load_a[$all_load[$i]->measure][$is];
            }
            if ($current_year != substr($current_application_date, 0, 4)) {
                $current_year = substr($current_application_date, 0, 4);
                $diff_month_count++;
            }
        }
    }

    if ($diff_month_count == 0) {
        return 0;
    } else {
        return $planned_application_count / $diff_month_count;
    }
}

/*
 * Returns list of not administrated support measure applications in total
 * 
 * @return array["SUP_MEASURE_CODE1", "SUP_MEASURE_CODE2", ..] => NOT_ADMINISTRATED_COUNT
 */

function get_all_left_applications() {
    $all_applications = get_historical_quantities();
    $left_over_applications = array();

    $all_support_measures = get_support_measures();
    $support_measure_entrys = count($all_support_measures["CODE"]);
    for ($i = 0; $i < $support_measure_entrys; $i++) {
        $left_over_applications[$all_support_measures["CODE"][$i]] = 0;
    }

    $application_entrys = count($all_applications["DATE"]);
    for ($i = 0; $i < $application_entrys; $i++) {

        $received = $all_applications["RECEIVED"][$i];
        $administrated = $all_applications["ADMINISTRATED"][$i];
        $received_administrated_diff = $received - $administrated;

        if ($received_administrated_diff > 0) {

            // For all not administrated applications, add to list
            for ($j = 0; $j < $received_administrated_diff; $j++) {
                $left_over_applications[$all_applications["MEASURE_CODE"][$i]] += 1;
            }
        } elseif ($received_administrated_diff < 0) {

            // For all administrated applications, delete from list
            for ($j = 0; $j != $received_administrated_diff; $j--) {
                if ($left_over_applications[$all_applications["MEASURE_CODE"][$i]] - 1 >= 0) {
                    $left_over_applications[$all_applications["MEASURE_CODE"][$i]] -= 1;
                } else {
                    $left_over_applications[$all_applications["MEASURE_CODE"][$i]] = 0;
                }
            }
        }
    }

    return $left_over_applications;
}
?>